import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'accountNumberM',

  state: {},
  effects: {
    // 获取存款总行下拉列表
    *getbankAccountList({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.getbankAccountList, payload);
      if (response && response.code === 0) {
        let temp = [];
        response.data.dataSource &&
          response.data.dataSource.forEach((item) => {
            temp.push({ value: item.lbankNo + ' ' + item.vcBankName, title: item.vcBankName });
          });
        callback('success', temp);
      } else {
        callback('fail');
        message.error(`查询存款总行下拉列表失败，失败原因：${response?.msg}`);
        console.error(`查询存款总行下拉列表失败，失败原因：${response?.msg}`, '接口名称： queryBank 接口：instrument-server/depositBank/queryBank');
      }
    },
    // 获取产品下拉列表
    *getProductList({ payload, callback }, { call, put }) {
      const response = yield call(api.getProducts, payload);
      if (response && response.success === true) {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(`获取产品下拉列表失败，失败原因：${response?.errorMsg}`);
        console.error(`获取产品下拉列表失败，失败原因：${response?.errorMsg}`, '接口名称： list 接口：portfolio-server/portfolioInfo/list');
      }
    },
    // 新增存款账号
    *addAccount({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.addAccount, payload);
      if (response && response.code === 0) {
        callback('success', response);
        message.success('新增存款账号成功');
      } else {
        callback('fail');
        message.error(`新增存款账号失败，失败原因：${response?.msg}`);
        console.error(`新增存款账号失败，失败原因：${response?.msg}`, '接口名称： saveOrUpdateAccount 接口：portfolio-server/bankAccount/saveOrUpdateAccount');
      }
    },
    // 根据Id编辑存款账号
    *editAccountById({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.editAccountById, payload);
      if (response && response.code === 0) {
        callback('success', response);
        message.success('编辑存款账号成功');
      } else {
        callback('fail');
        message.error(`编辑存款账号失败，失败原因：${response?.msg}`);
        console.error(`编辑存款账号失败，失败原因：${response?.msg}`, '接口名称： saveOrUpdateAccount 接口：portfolio-server/bankAccount/saveOrUpdateAccount');
      }
    },
    // 根据Id查询账号历史记录
    *getHistoryById({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.getHistoryById, payload);
      if (response && response.code === 0) {
        response.data.dataSource &&
          response.data.dataSource.forEach((value, index) => {
            value.rowKey = index;
          });
        response.data.columns &&
          response.data.columns.forEach((item) => {
            if (item.title == '利率') {
              item.type = 'rate';
            }
          });
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(`查询历史记录失败，失败原因：${response?.msg}`);
        console.error(`查询历史记录失败，失败原因：${response?.msg}`, '接口名称： getAccountHistory 接口：portfolio-server/bankAccount/getAccountHistory');
      }
    },
    // 根据Id删除存款账号
    *deleteAccountById({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.deleteAccountById, payload);
      if (response && response.code === 0) {
        callback('success', response);
        message.success(response.msg || '删除成功');
      } else {
        callback('fail');
        message.error(`删除存款账号失败，失败原因：${response?.msg}`);
        console.error(`删除存款账号失败，失败原因：${response?.msg}`, '接口名称： deleteBankAccount 接口：portfolio-server/bankAccount/deleteBankAccount');
      }
    },
    //获取存款账号信息
    *getAccountData({ payload, callback }, { call, put }) {
      const response = yield call(api.accountNumber.getAccountData, payload);
      if (response && response.code === 0) {
        response.data.columns.forEach((item) => {
          if (item.title == '活期利率') {
            item.type = 'rate';
          }
        });
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(`获取存款账号数据失败，失败原因：${response?.msg}`);
        console.error(`获取存款账号数据失败，失败原因：${response?.msg}`, '接口名称： getBankAccount 接口：portfolio-server/bankAccount/getBankAccount');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
